package com.ce.webframework.sec.dao.impl;

import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import com.ce.webframework.sec.dao.ISecScheduleDao;
import com.ce.webframework.sec.model.SecExcludeSchedDO;
import com.ce.webframework.sec.model.SecScheduleDO;
import com.sitechasia.webx.core.dao.hibernate3.HibernateDomainObjectDao;

public class SecScheduleDaoImpl extends HibernateDomainObjectDao<SecScheduleDO> implements ISecScheduleDao{


		/**
		 * 功能描述:增加日程记录
		 *
		 * @param schDO
		 *            SecScheduleDO 实体对象
		 * @return SecScheduleDO
		 */
	
	
		public SecScheduleDO addSecSchedule(SecScheduleDO schDO) {
			doSave(schDO);
			return schDO;
		}
	
		/**
		 * 功能描述:更新日程记录
		 *
		 * @param calDO
		 *            SecScheduleDO 实体对象
		 * @return void
		 *            增加后的对象
		 */
		public SecScheduleDO updateSecSchedule(SecScheduleDO schDO) {
			genericSave(SecScheduleDO.class, schDO);
			return schDO;
		}
	
		/**
		 * 功能描述:按主键Id 删除日程记录
		 *
		 * @param scheduleId
		 *            SecScheduleDO 的主键
		 * @return boolean 删除成功是为true，失败时为false
		 */
	
		public boolean deleteSecSchedule(Long scheduleId) {
			     doDeleteById(scheduleId);
			     return true;
		}
	
	
		/**
		 * 功能描述:根据scheduleId查找SecScheduleDO对象
		 *
		 * @param scheduleId
		 *            SecScheduleDO对象的Id
		 * @return SecScheduleDO  id对应的SecScheduleDO对象
		 */
		public SecScheduleDO getSecScheduleById(Long scheduleId) {
			 String  hql=" FROM SecScheduleDO sch WHERE sch.scheduleId=?";
			 Query query=getSession().createQuery(hql);
			 query.setParameter(0, scheduleId);
			 List schList = query.list();
				if (schList == null || schList.size() == 0) {
					return null;
				}
				return (SecScheduleDO) schList.get(0);
		}
	
		/**
		 * 功能描述:根据scheduleId查找SecScheduleDO对象
		 *
		 * @param scheduleId
		 *            SecScheduleDO对象的Id
		 * @param remindTime
		 *            remindTime
		 * @return SecScheduleDO  id对应的SecScheduleDO对象
		 */
		public SecScheduleDO getSecScheduleById(Long scheduleId,Date remindTime) {
			 String  hql=" FROM SecScheduleDO sch WHERE sch.scheduleId=?";
			 Query query=getSession().createQuery(hql);
			 query.setParameter(0, scheduleId);
			 List schList = query.list();
				if (schList == null || schList.size() == 0) {
					return null;
				}
		    SecScheduleDO schDo=(SecScheduleDO)schList.get(0);
		    //如果时间没有修改,返回该对象
		    if(schDo.getStartTime().equals(remindTime)){
		    	return schDo;
		    }else{
		    	return null;
		    }
		}
	
	
	
		/**
		 * 功能描述:根据用户Id和日期 查找 SecScheduleDO对象列表
		 *
		 * @param userId
		 *            用户的Id
		 * @param beginDate
		 *            开始时间
		 * @param endDate
		 *            开始时间       
		 * @return List<SecScheduleDO> 对象
		 *
		 * 返回符合条件的查询对象列表
		 */
	
		public List<SecScheduleDO> getSecScheduleList(Long userId, Date beginDate, Date endDate) {
		    System.out.println(" getSecScheduleList --beginDate :" + beginDate.toString() + ",endDate:" + endDate.toString());
	
	        StringBuilder  hql=new StringBuilder(" FROM SecScheduleDO sch ");
	        hql.append(" WHERE sch.userId=:userId");
	        hql.append(" AND sch.startTime<=:endDate AND sch.endTime>=:beginDate");
	        hql.append(" AND sch.scheduleId");
	        hql.append(" NOT IN (SELECT exclude.secSchedule.scheduleId FROM SecExcludeSchedDO exclude");
	        hql.append(" WHERE exclude.userId=:userId AND exclude.excludeDate=:beginDate)");
	        Query query=getSession().createQuery(hql.toString());
	        query.setParameter("userId", userId);
	        query.setParameter("endDate", endDate);
	        query.setParameter("beginDate", beginDate);
	
	         List<SecScheduleDO> returnList = query.list();
	         System.out.println(" return List="+returnList.size()+" query.list()"+query.list().size());
	     	return returnList;
	
		}
	
	
		/**
		 * 功能描述:根据用户传入的该月的第一天的日期和最后一天的日查询满足条件的记录
		 *
		 * @param userId
		 *            用户的Id
		 * @param beginDate
		 *           开始时间(月的第一天)
		 * @param endDate
		 *           结束日期(月的最后一天)
		 * @return List<SecScheduleDO>
		 *
		 * 返回符合条件的查询对象列表
		 */
	
		public List<SecScheduleDO> getMonthHaveStyle(Long userId,Date beginDate,Date endDate){
		    System.out.println(" changeMonthStyle =beginDate :" + beginDate.toString() + ",endDate:" + endDate.toString());
	
		    //开始时间必须小于月末,结束时间必须大于月初
			StringBuilder hql =new StringBuilder();
			hql.append(" FROM SecScheduleDO sch WHERE sch.userId=?");
			hql.append(" AND sch.startTime<=?");
			hql.append(" AND sch.endTime>=?  ");
			Query query = getSession().createQuery(hql.toString());
			query.setParameter(0, userId);
			query.setParameter(1, endDate);
			query.setParameter(2, beginDate);
			List<SecScheduleDO> list = query.list();
	
			System.out.println(" list  size="+list.size());
			return list;
	
		}
	
		
		/**
		 * 功能描述:根据用户传入的该月的第一天的日期和最后一天的日查询满足条件日程循环排除表的记录
		 *
		 * @param userId
		 *            用户的Id
		 * @param beginDate
		 *           开始时间(月的第一天)
		 * @param endDate
		 *           结束日期(月的最后一天)
		 * @return List<SecExcludeSchedDO>
		 *
		 * 返回符合条件的查询对象列表
		 */
		public List<SecExcludeSchedDO> getMonthExclueStyle(Long userId,Date beginDate,Date endDate){
			
	         //排除修改和删除的记录
			StringBuilder hqlExclude =new StringBuilder();
			hqlExclude.append(" FROM SecExcludeSchedDO exclude WHERE exclude.userId=?");
			hqlExclude.append("  AND exclude.excludeDate BETWEEN ? AND ?)");
			Query queryExclude = getSession().createQuery(hqlExclude.toString());
			queryExclude.setParameter(0, userId);
			queryExclude.setParameter(1, beginDate);
			queryExclude.setParameter(2, endDate);
			List<SecExcludeSchedDO> listExclude = queryExclude.list();
	
			System.out.println("listExclude size="+listExclude.size());
			return listExclude;
		}
			
	
	
		/**
		 * 功能描述:根据 scheduleId 删除 SecExcludeSchedDO 表的记录
		 * @param scheduleId
		 *           SecScheduleDO 的主键
		 * @return  void
	     */
		public void delSecExcludeSchedDO(Long  scheduleId){
			 String hql = "DELETE SecExcludeSchedDO  WHERE secSchedule.scheduleId=?";
			 Query query=getSession().createQuery(hql);
			 query.setParameter(0, scheduleId);
			 query.executeUpdate();
		}
	
	
	
		/**
		 * 功能描述:增加一条 记录到 日历 循环排除表
		 *
		 * @param SecExcludeSchedDO
		 *            SecExcludeSchedDO 实体对象
		 * @return void
		 */
		public void addSecExcludeSchedDO(SecExcludeSchedDO schExcludeDO) {
			genericSave(SecExcludeSchedDO.class, schExcludeDO);
		}

}
